package com.alibaba.citrus.ots.platform.enhance.repository.impl;

import com.alibaba.citrus.ots.platform.enhance.repository.DictionaryRepository;
import com.alibaba.lattice2.epoch.sdk.model.Result;
import com.epoch.app.otsplatformenhance.dictionaryquery.service.DictionaryQueryService;
import com.epoch.app.otsplatformenhance.model.dto.BaseEnumDTO;
import com.epoch.app.otsplatformenhance.model.dto.DictValueSDO;
import com.epoch.app.otsplatformenhance.model.dto.QueryDictionaryListRequest;
import com.epoch.app.otsplatformenhance.model.dto.QueryDictionaryRequest;
import com.epoch.app.otsplatformenhance.sdo.DictionarySDO;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @description:
 * @author: qianlei
 * @create: 2021/10/16 17:06
 */
@Service
@Primary
public class DictionaryQueryServiceImpl implements DictionaryQueryService {
    //预报单类型
    public static final String FORECAST_TAG = "forecastTag";
    //单据原因
    public static final String ORDER_REASON = "orderReason";
    //拒绝原因
    public static final String ORDER_REFUSE_REASON = "refuseReason";
    //销售订单类型
    public static final String SALE_ORDER_TAG = "saleOrderTag";
    @Autowired
    private DictionaryRepository dictionaryRepository;
    @Override
    public Result<List<BaseEnumDTO>> queryValues(QueryDictionaryRequest request) {
        DictionarySDO sdo = dictionaryRepository.querySingle(request);
        if (sdo == null || CollectionUtils.isEmpty(sdo.getDictValueList())){
            return Result.listEmptySuccess();
        }
        List<BaseEnumDTO> enumDTOS= sdo.getDictValueList().stream().map(dictValueSDO -> {
            BaseEnumDTO enumDTO = new BaseEnumDTO();
            enumDTO.setLabel(dictValueSDO.getValueName());
            enumDTO.setValue(dictValueSDO.getValueData());
            return enumDTO;
        }).collect(Collectors.toList());
        return Result.listSuccess(enumDTOS,enumDTOS.size());
//        if (FORECAST_TAG.equals(request.getDictCode())){
//
//        }
//        if (SALE_ORDER_TAG.equals(request.getDictCode())){
//
//        }
//        if (ORDER_TICKET_REASON.equals(request.getDictCode())){
//
//        }
//        if (ORDER_REFUSE_REASON.equals(request.getDictCode())){
//
//        }
//        return null;
    }

    @Override
    public Result<DictionarySDO> querySingle(QueryDictionaryRequest request) {
        DictionarySDO sdo = dictionaryRepository.querySingle(request);
        if (sdo == null || CollectionUtils.isEmpty(sdo.getDictValueList())){
            return Result.success(null);
        }
        return Result.success(sdo);
    }

    @Override
    public Result<List<DictionarySDO>> pageQuery(QueryDictionaryListRequest request) {
        return dictionaryRepository.pageQuery(request);
    }

    @Override
    public Result<List<DictionarySDO>> queryAll() {
        return null;
    }

    @Override
    public Result<List<BaseEnumDTO>> queryMergeValueList(QueryDictionaryListRequest request) {
        List<DictValueSDO> sdoList = dictionaryRepository.queryDictValuesByCodes(request);

        if ( CollectionUtils.isEmpty(sdoList)){
            return Result.listEmptySuccess();
        }
        List<BaseEnumDTO> enumDTOS= sdoList.stream().map(dictValueSDO -> {
            BaseEnumDTO enumDTO = new BaseEnumDTO();
            enumDTO.setLabel(dictValueSDO.getValueName());
            enumDTO.setValue(dictValueSDO.getValueData());
            return enumDTO;
        }).collect(Collectors.toList());

        return Result.listSuccess(enumDTOS,enumDTOS.size());
    }

    @Override
    public Result<String> queryValueNameByCode(QueryDictionaryRequest queryDictionaryRequest){
        DictValueSDO sdo = dictionaryRepository.queryValueNameByCode(queryDictionaryRequest);
        if (sdo != null){
            return Result.success(sdo.getValueName());
        }
        return Result.success(null);
    }

}
